package com.youku.expression.ast;

import com.youku.api.ApiCaller;
import com.youku.expression.environment.Environment;

/**
 * 调用方法
 * 
 * @author loda
 *
 */
public class InvokeFunctionAst extends Ast {

	public InvokeFunctionAst(Ast var, Ast arguments) {
		super(var, arguments);
	}

	/**
	 * 方法
	 * 
	 * @return
	 */
	public Ast function() {
		return getChild(0);
	}

	/**
	 * 参数列表
	 * 
	 * @return
	 */
	public Ast arguments() {
		return getChild(1);
	}

	@Override
	public String toString() {
		return function() + "(" + arguments() + ")";
	}

	@Override
	public Object eval(Environment environment) {

		IdentifierAst primary = (IdentifierAst) function();

		String funName = primary.getTokenValue();

		Object[] args = (Object[]) arguments().eval(environment);

		return ApiCaller.call(funName, toString(), args);
	}

}
